<?php
/**
 * Created by PhpStorm.
 * User: tt
 * Date: 2017/12/22
 * Time: 19:32
 */

namespace app\admin\controller;

use app\admin\logic\Qudao as Qudao_logic;
use think\Request;
use think\File;
use think\Db;
use nx\Data;

class Qudao extends Admin
{
    /**
     * 渠道列表
     * @return mixed
     */
    public function index()
    {
        $Qudaos = new Qudao_logic();
        $qudao = $Qudaos->getAllQudaoToTree();
        foreach ($qudao as $k => $v) {
            $qudao[$k]['xxqdclassify'] = Db::name('classify')->where(['pid' => 50, 'id' => $v['xxqdclassify']])->field('id', 'title')->find();
        }
//        dump($qudao);
        $qudao = menu_left($qudao);
        $this->assign('qudao', $qudao);
        return $this->fetch();
    }

    /**
     * 添加渠道
     * @return mixed
     */

    public function add()
    {
        $Qudao_logic = new Qudao_logic();
        if ($this->request->isPost()) {
            $data = input('post.');
            $data['title'] = trim($data['title']);
            $data['create_time'] = time();


            if ($data['type'] == 1) {
                $data['xxqdclassify'] = 0;
                $result = $Qudao_logic->addQudao($data);
            } else {
                $result = $Qudao_logic->addQudao($data);
            }
            if (false === $result['result']) {
                $this->error('新增失败');
            } else {
                $this->success('新增成功');
            }
        } else {
            $classify_pid = Db::name('classify')->where('pid', 50)->select();
            $arr = $Qudao_logic->getAllText();
            $this->assign('classify_pid', $classify_pid);
            $this->assign('pid', $arr);
            return $this->fetch();
        }
    }


    /**
     * 编辑渠道
     * @return mixed
     */
    public function edit()
    {
        $Qudao_logic = new Qudao_logic();
        $arr = $Qudao_logic->getAllText();
        $this->assign('pid', $arr);
        if ($this->request->isPost()) {
            $data = input('post.');
            if ($data['type'] == 1) {
                $data['xxqdclassify'] = 0;
                $result = $Qudao_logic->editQudao($data);
            } else {
                $result = $Qudao_logic->editQudao($data);
            }
            if ($result['result'] === true) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }
        } else {
            $id = input('id');
            $Qudao = Db::name('qudao')->where('id', $id)->find();
            $classify_pid = Db::name('classify')->where('pid', 50)->select();
            $this->assign('qudao', $Qudao);
            $this->assign('classify_pid', $classify_pid);
            return $this->fetch();
        }
    }

    /**
     * 删除渠道
     */
    public function del()
    {
        $id = $this->request->param('id', 0);
        $Qudao_logic = new Qudao_logic();
        $result = $Qudao_logic->delQudao($id);
        if ($result['result'] === true) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 设置开启或禁用
     * @return mixed
     * @author: tt
     */
    public function setStatus()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('ID丢失');
        }
        $info = Db::name('qudao')->where('id', $id)->field('id,status')->find();
        $status = [1, 0];
        $data['id'] = $id;
        $data['status'] = $status[$info['status']];
        $res = Db::name('qudao')->update($data);
        if ($res) {
            Cache::rm('allqudaos');
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
        }
    }


    /**
     * 设置渠道绑定来源
     * @return mixed
     */
    public function setstates()
    {
        if ($this->request->isPost()) {
            $authdata = $this->request->post();
            if (empty($authdata['id'])) {
                $this->error('参数丢失');
            } else {
                $data['id'] = $authdata['id'];
                $data['rule'] = implode(',', $this->request->post('rule/a'));
                $res = model('qudao')->allowField(true)->isUpdate()->save($data);
                if ($res) {
                    $this->success('操作成功');
                } else {
                    $this->error(model('qudao')->getError());
                }
            }
        } else {
            $Qudaoid = input('id', 0, 'int');
            $this->assign('title','渠道绑定来源');
            $Qudao = Db::name('qudao')->where(['id' => $Qudaoid])->find();//当前类别
            $auth_group = Db::name('origin')->cache(6000)->where('status',1)->select();//获取所有的角色
            $this->assign('qudao', $Qudao);
            $this->assign('origin', $auth_group);
            return $this->fetch();
        }
    }

    /**
     * 设置渠道绑定班级
     * @return mixed
     */
    public function bindbanji($id = '')
    {
        if ($this->request->isPost()) {
            $authdata = $this->request->post();
            $authdata['rule'] = implode(',',$authdata['rule']);
//            dump($authdata);
            $a = db('qudao')->where('id',$authdata['id'])->update(['banji_rule'=>$authdata['rule']]);
//            dump($a);
            $this->success('绑定成功');
        }else{
            if (empty($id)){
                $this->error('参数丢失');
            }
            $arr = db('qudao')->where('id',$id)->find();
            $data = Db::name('banji')->cache(6000)->where('status',1)->select();
            $this->assign('data',$data);
            $this->assign('arr',$arr);
            return $this->fetch();
        }
    }

}
